﻿using System;
using System.Globalization;

namespace CustomerContactSystem.WebForms.Customers
{
    public partial class Edit : BasePage
    {
        public string FirstNameQry
        {
            get { return Request.QueryString["firstname"] ?? ""; }
        }
        public string SecondNameQry
        {
            get { return Request.QueryString["secondname"] ?? ""; }
        }
        public string AddressQry
        {
            get { return Request.QueryString["address"] ?? ""; }
        }
        public bool ContactableOnlyQry
        {
            get { return Request.QueryString["contactable"] != null && bool.Parse(Request.QueryString["contactable"]); }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            GetCustomer();
            ReturnToSearchLink.NavigateUrl = RedirectToSearchPath(QueryStringOrEmpty("id"));
        }

        private void GetCustomer() {
            var customerId = QueryStringOrZero("id");

            if (customerId == 0)
            {
                return;
            }

            var customer = CustomerRepository.GetById(customerId);

            if (customer == null) {
                return;
            }

            FirstName.Text = customer.FirstName;
            SecondName.Text = customer.SecondName;
            Address1.Text = customer.Address1;
            Address2.Text = customer.Address2;
            Address3.Text = customer.Address3;
            Address4.Text = customer.Address4;
            Address5.Text = customer.Address5;
            Email.Text = customer.Email;
            Phone.Text = customer.Phone;
            Fax.Text = customer.Fax;
            Contactable.Checked = customer.CanBeContactedForFeedback;
            Flagged.Checked = customer.Flagged;
        }

        private string RedirectToSearchPath(string customerId)
        {
            var redirectPath = "~/Customers/Search.aspx?firstname=" + FirstNameQry +
                               "&secondname=" + SecondNameQry + "&address=" + AddressQry + "&contactable=" +
                               ContactableOnlyQry;

            if (customerId.Length > 0)
            {
                redirectPath += "#" + customerId;
            }

            return redirectPath;
        }

        protected void SaveCustomerClick(object sender, EventArgs e)
        {
            var customerId = QueryStringOrZero("id");

            if (customerId == 0)
            {
                return;
            }

            var customer = CustomerRepository.GetById(customerId);

            if (customer == null)
            {
                return;
            }

            customer.FirstName = FirstName.Text;
            customer.SecondName = SecondName.Text;
            customer.Address1 = Address1.Text;
            customer.Address2 = Address2.Text;
            customer.Address3 = Address3.Text;
            customer.Address4 = Address4.Text;
            customer.Address5 = Address5.Text;
            customer.Email = Email.Text;
            customer.Phone = Phone.Text;
            customer.Fax = Fax.Text;
            customer.CanBeContactedForFeedback = Contactable.Checked;
            customer.Flagged = Flagged.Checked;

            if (!IsValidObject(customer, SaveCustomerResultMessage)) {
                return;
            }

            CustomerRepository.Update(customer);
            Response.Redirect(RedirectToSearchPath(customer.Id.ToString(CultureInfo.InvariantCulture)));
        }
    }
}